﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using System.Text;

namespace HRProject.Salary
{
    public partial class AjaxResponsibleSalary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sAction = Request.Params["action"];
            string sJobTitleId = Request.Params["jobTitleId"];
            string sYear = Request.Params["year"];
            string sMonth = Request.Params["month"];
            string sPageIndex = Request.Params["pageIndex"];

            string sAgentId = Request.Params["agentId"];

            StringBuilder result = new StringBuilder();
            int errorCode = 0;
            string error = string.Empty;
            string status = "true";

            try
            {
                int year = int.Parse(sYear);
                int month = int.Parse(sMonth);
                DateTime date = new DateTime(year, month, 1);
                if ("get_monthly_responsible_salary".Equals(sAction))
                {
                    Guid jobTitleId = new Guid(sJobTitleId);
                    int pageIndex = int.Parse(sPageIndex);

                    BLJobTitle jobTitleFilter = new BLJobTitle();
                    jobTitleFilter.Id = jobTitleId;

                    AgentDAO agentDao = new AgentDAO();
                    int totalAgents = agentDao.CountAgent(jobTitleFilter, Guid.Empty, string.Empty, Constant.DefaultMinDate, Constant.DefaultMaxDate);
                    int totalPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totalAgents) / Convert.ToDouble(ContentProvider.PageSize)));

                    if (pageIndex < 1)
                    {
                        pageIndex = 1;
                    }
                    if (pageIndex > totalPage)
                    {
                        pageIndex = totalPage;
                    }

                    ResponsibleSalaryDAO salaryDao = new ResponsibleSalaryDAO();
                    BLMonthlyResponsibleSalaryTable salaryTable = salaryDao.Collect(jobTitleFilter, Guid.Empty, string.Empty,
                        date, totalPage, ContentProvider.PageSize, pageIndex);

                    StringBuilder list = new StringBuilder();
                    int j = 0;
                    string format = new string('0', Convert.ToString(salaryTable.AgentResponsibleSalaryInfos.Count).Length);
                    list.Append("[");
                    foreach (BLAgentMonthlyResponsibleSalary info in salaryTable.AgentResponsibleSalaryInfos)
                    {
                        if (j > 0)
                        {
                            list.Append(",");
                        }
                        list.Append("{\"agentId\":\"");
                        list.Append(info.Agent.Id);
                        list.Append("\",\"order\":\"");
                        list.Append(info.Order.ToString(format));
                        list.Append("\",\"fullName\":\"");
                        list.Append(info.Agent.FullName);
                        list.Append("\",\"responsibleLevel\":\"");
                        list.Append(info.ResponsibleLevel.InfoNumberValue);
                        list.Append("\",\"description\":\"");
                        list.Append(info.ResponsibleLevel.Description);
                        list.Append("\",\"totalResponsibleSalary\":\"");
                        list.Append(info.TotalResponsibleSalaryString);
                        list.Append("\"}");
                        j++;
                    }
                    list.Append("]");

                    result.Append("{\"status\":true,\"totalPage\":\"");
                    result.Append(totalPage);
                    result.Append("\",\"pageIndex\":\"");
                    result.Append(pageIndex);
                    result.Append("\",\"list\":");
                    result.Append(list);
                    result.Append("}");
                }
                else if ("update_monthly_responsible_salary".Equals(sAction))
                {
                    string sResponsibleLevel = Request.Params["responsibleLevel"];
                    string sDescription = Request.Params["description"];
                    int responsibleLevel;
                    if (int.TryParse(sResponsibleLevel, out responsibleLevel))
                    {
                        int updateResult = 0;
                        BLMonthlyResponsibleSalaryTable monthRef = new BLMonthlyResponsibleSalaryTable(year, month);
                        BLAgentMonthlyResponsibleSalary responsibleSalaryInfo = new BLAgentMonthlyResponsibleSalary(monthRef);
                        responsibleSalaryInfo.Agent.Id = new Guid(sAgentId);
                        responsibleSalaryInfo.ResponsibleLevel.InfoNumberValue = int.Parse(sResponsibleLevel);
                        responsibleSalaryInfo.ResponsibleLevel.Description = sDescription;
                        ResponsibleSalaryDAO salaryDao = new ResponsibleSalaryDAO();
                        updateResult = salaryDao.Update(responsibleSalaryInfo, ref errorCode);
                        if (errorCode == 12)
                        {
                            error = ContentProvider.GetMessage("NotAuthorizedDetail")
                                .Replace("{0}", ContentProvider.GetString("UpdateResponsibleSalary"));
                            status = "false";
                        }
                        result.Append("{\"status\":");
                        result.Append(status);
                        result.Append(",\"error\":\"");
                        result.Append(error);
                        if ("true".Equals(status))
                        {
                            result.Append("\",\"responsibleLevel\":\"");
                            result.Append(responsibleSalaryInfo.ResponsibleLevel.InfoNumberValue);
                            result.Append("\",\"description\":\"");
                            result.Append(responsibleSalaryInfo.ResponsibleLevel.Description);
                            result.Append("\",\"totalResponsibleSalary\":\"");
                            result.Append(responsibleSalaryInfo.TotalResponsibleSalaryString);
                        }
                        result.Append("\"}");
                    }
                    else
                    {
                        status = "false";
                        error = ContentProvider.GetMessage("InvalidNumberFormat");
                        result.Append("{\"status\":");
                        result.Append(status);
                        result.Append(",\"error\":\"");
                        result.Append(error);
                        result.Append("\"}");
                    }
                }
            }
            catch (Exception exp)
            {
                Console.Out.WriteLine(exp.Message);
            }

            Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
            Response.Write(result);
            Response.End();
        }
    }
}
